package com.qpkj.web.controller.oms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qpkj.oms.domain.*;
import com.qpkj.oms.parameter.AchievementexportVo;
import com.qpkj.oms.parameter.TaskAddVo;
import com.qpkj.oms.parameter.TaskToppingVo;
import com.qpkj.oms.service.IOmsContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.qpkj.common.annotation.Log;
import com.qpkj.common.core.controller.BaseController;
import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.enums.BusinessType;
import com.qpkj.oms.service.IOmsTaskService;
import com.qpkj.common.utils.poi.ExcelUtil;
import com.qpkj.common.core.page.TableDataInfo;

/**
 * 任务Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/oms/omstask")
public class OmsTaskController extends BaseController {
    @Autowired
    private IOmsTaskService omsTaskService;
    @Autowired
    private IOmsContractService omsContractService;

    /**
     * 查询任务列表
     */
    //@PreAuthorize("@ss.hasPermi('oms:omstask:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsTask omsTask) {
        startPage();
        List<OmsTask> list = omsTaskService.selectOmsTaskList(omsTask);
        return getDataTable(list);
    }
    /**
     * 查询合同列表
     */
    @GetMapping("/contractList")
    public AjaxResult contractList() {
        List<OmsContract> list = omsContractService.selectContractList();
        return AjaxResult.success(list);
    }

    /**
     * 导出任务列表
     */
    //@PreAuthorize("@ss.hasPermi('oms:omstask:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsTask omsTask) {
        List<OmsTask> list = omsTaskService.selectOmsTaskListExport(omsTask);
        ExcelUtil<OmsTask> util = new ExcelUtil<OmsTask>(OmsTask.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:omstask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return success(omsTaskService.selectOmsTaskByTaskId(taskId));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('oms:omstask:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TaskAddVo taskAddVo) {
        return omsTaskService.insertOmsTask(taskAddVo);
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('oms:omstask:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsTask omsTask) {
        return toAjax(omsTaskService.updateOmsTask(omsTask));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('oms:omstask:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(omsTaskService.deleteOmsTaskByTaskIds(taskIds));
    }

    @PreAuthorize("@ss.hasPermi('oms:omstask:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/taskTopping")
    public AjaxResult taskTopping(@Validated @RequestBody TaskToppingVo taskToppingVo) {
        return omsTaskService.taskTopping(taskToppingVo);
    }

    @PreAuthorize("@ss.hasPermi('oms:omstask:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/taskCancel")
    public AjaxResult taskCancel(@RequestParam("taskId") Long taskId) {
        return omsTaskService.taskCancel(taskId);
    }

    /**
     * 修改任务计划完成时间
     *
     * @param taskId
     * @param time
     * @return
     */
    // @PreAuthorize("@ss.hasPermi('oms:omstask:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTaskDate")
    public AjaxResult updateTaskDate(@RequestParam("taskId") Long taskId, @RequestParam("time") String time) {
        return omsTaskService.updateTaskDate(taskId, time);
    }
    /**
     * 修改交付客户
     *
     * @param taskId
     * @param inputDeliver
     * @return
     */
    // @PreAuthorize("@ss.hasPermi('oms:omstask:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeliver")
    public AjaxResult updateDeliver(@RequestParam("taskId") Long taskId, @RequestParam("inputDeliver") String inputDeliver) {
        return omsTaskService.updateDeliver(taskId, inputDeliver);
    }

    /**
     * 获取任务步骤
     *
     * @param taskId 任务ID
     * @param type   (0领料1擦球2电镀3彩绘4包装5完成)
     * @return
     */
    @GetMapping(value = "/getTaskStep")
    public TableDataInfo getTaskStep(@RequestParam("taskId") Long taskId, @RequestParam("type") Integer type) {
        return getDataTable(omsTaskService.getTaskStep(taskId, type));
    }

    /**
     * 任务环节操作
     *
     * @param taskId
     * @param stepId
     * @param type
     * @return
     */
    @PostMapping(value = "/taskOperation")
    public AjaxResult taskOperation(@RequestParam("taskId") Long taskId, @RequestParam("stepId") Long stepId
            , @RequestParam("type") Integer type) {
        return omsTaskService.taskOperation(taskId, stepId, type);
    }

    /**
     *
     * @param productId  生产目标的物料Id
     * @param complementNum 需要补数的数量
     * @param productNum  该任务生产目标的数量
     * @param taskId  该任务的任务id
     * @param isBuShu  是否补数
     * @return
     */
    @PostMapping("/complement")
    public AjaxResult taskcomplement(@RequestParam("productId") Long productId, @RequestParam("complementNum") Long complementNum,@RequestParam("productNum") Long productNum,@RequestParam("taskId") Long taskId,@RequestParam("isBuShu") String isBuShu) {
        return omsTaskService.taskcomplement(productId, complementNum, productNum,taskId,isBuShu);
    }
    /**
     * 半成品任务
     *
     * @param taskId 任务ID
     * @return
     */
    @GetMapping(value = "/taskProductList")
    public AjaxResult taskProductList(@RequestParam("taskId") Long taskId) {
        return omsTaskService.taskProductList(taskId);
    }

    /**
     * 任务——所有商品获取
     *
     * @param taskId 任务ID
     * @return
     */
    @GetMapping(value = "/taskAllProductList")
    public AjaxResult taskAllProductList(@RequestParam("taskId") Long taskId) {
        return omsTaskService.taskAllProductList(taskId);
    }
    /**
     * 根据任务id查询当前关联的合同编号
     *
     * @param taskId 任务ID
     * @return
     */
    @GetMapping(value = "/taskContract")
    public AjaxResult taskContract(@RequestParam("taskId") Long taskId) {
        return omsTaskService.taskContract(taskId);
    }
    /**
     * 任务商品导出
     *
     * @param response
     * @param taskId
     */
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/taskAllParoductexport")
    public void taskAllParoductexport(HttpServletResponse response, @RequestParam("taskId") Long taskId) {
        List<OmsTaskProduct> list = omsTaskService.getTaskProductListByTaskId(taskId);
        ExcelUtil<OmsTaskProduct> util = new ExcelUtil<OmsTaskProduct>(OmsTaskProduct.class);
        util.exportExcel(response, list, "任务商品数据");
    }

    /**
     * 任务半成品导出
     *
     * @param response
     * @param taskId
     */
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/taskParoductexport")
    public void taskParoductexport(HttpServletResponse response, @RequestParam("taskId") Long taskId) {
        List<OmsProductNeeds> list = omsTaskService.getTaskProductNeedsListByTaskId(taskId);
        ExcelUtil<OmsProductNeeds> util = new ExcelUtil<OmsProductNeeds>(OmsProductNeeds.class);
        util.exportExcel(response, list, "任务半成品数据");
    }

    /**
     * 人工业绩导出
     *
     * @param response
     */
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/taskAchievementexport")
    public void taskAchievementexport(HttpServletResponse response, AchievementexportVo achievementexportVo) {
        List<OmsAchievement> list = omsTaskService.queryTaskAchievement(achievementexportVo.getTaskId(), achievementexportVo.getChejian());
        ExcelUtil<OmsAchievement> util = new ExcelUtil<OmsAchievement>(OmsAchievement.class);
        util.exportExcel(response, list, "人工业绩数据");
    }
}
